﻿package code {
	// class replicates a spring
	public class Spring extends GameObject {
		
		private var springLength = 10;  // length that the spring is
		private var spring = 2;  // spring constant
		private var friction = .95;  // friction of the spring
		public function Spring(shapeType:String, radius:Number, sides:Number, color:uint, thickness:Number, hollow:Boolean, angle:Number, lineHeight:int = 0) {
			super(shapeType, radius, sides, color, thickness, hollow, angle, lineHeight);
		}
		
		// springs to a position and applies force to the ball
		public function springTo(ballA:Ball, pointA:Vertex):void
		{
			var dx:Number = pointA.x - ballA.x;
			var dy:Number = pointA.y - ballA.y;
			var angle:Number = Math.atan2(dy, dx);
			var targetY:Number = pointA.y - Math.sin(angle) * springLength;
			ballA.vy += (targetY - ballA.y) * spring;			
			ballA.vy *= friction;
			
			
			ballA.y += ballA.vy;
			y = pointA.y;
		}

	}
	
}
